package com.ebelter.nb.ui.activitys;

import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.WeightSelectDialog;
import com.ebelter.btcomlib.views.WheelView;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.btcomlib.views.dialogs.WheelViewDialog;
import com.ebelter.btcomlib.views.dialogs.picktime2.CustomDatePicker;
import com.ebelter.nb.R;
import com.ebelter.nb.constants.NbConstants;
import com.ebelter.nb.model.http.HttpResponse2;
import com.ebelter.nb.model.http.request.NbNetUtils;
import com.ebelter.nb.model.http.response.NbBaseResponse;
import com.ebelter.nb.utils.NbUtitls;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import static com.ebelter.btcomlib.models.https.IHttpRequestField.birthday;
import static com.ebelter.btcomlib.views.dialogs.TextTipDialog.Gravity_BUTTOM;

public class PersonInfoActivity extends NBBaseActivity implements View.OnClickListener {


    public static final int REQUEST_CODE = 24;
    private static final String TAG = "PersonInfoActivity";
    private ImageView top_left_iv;
    private TextView title_tv;
    private TextView rightTitle_tv;

    private ImageView p_top_pic;

    private TextView sex2_p_tv;
    private TextView zhiye_p_tv;
    private TextView bir2_p_tv;
    private TextView height2_p_tv;
    private TextView weight2_p_tv;

    private ImageView sex_p_iv;
    private ImageView zhiye_p_iv;
    private ImageView bir1_p_iv;
    private ImageView height_p_iv;
    private ImageView weight_p_iv;

    int sex;
    int profession;
    String birthday;
    private int height = 100;
    private float weight = 0;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_person_info;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
        setListener();
    }

    @Override
    protected void initData() {
        super.initData();


        sex = UserSpUtil.readInt(NbConstants.User.sex,0);//0男1女;
        sex2_p_tv.setText(sex==0?"男":"女");
        p_top_pic.setImageResource(sex==0?R.drawable.ic_man:R.drawable.ic_women);

        profession = UserSpUtil.readInt(NbConstants.User.profession,0);//0普通1运动员
        zhiye_p_tv.setText(profession==0?"普通工作者":"运动员");

        birthday = UserSpUtil.readString(NbConstants.User.birthday);
        bir2_p_tv.setText(birthday);

        int height2= UserSpUtil.readInt(NbConstants.User.height);
        if (height2>0){
            height=height2;
        }
        height2_p_tv.setText(height2+" CM");

        float weight2= UserSpUtil.readFloat(NbConstants.User.weight);
        weight=weight2;
        weight2_p_tv.setText(weight2+" Kg");
    }

    private void setListener() {
        top_left_iv.setOnClickListener(this);
        rightTitle_tv.setOnClickListener(this);
        sex2_p_tv.setOnClickListener(this);
        bir2_p_tv.setOnClickListener(this);
        height2_p_tv.setOnClickListener(this);
        weight2_p_tv.setOnClickListener(this);
        sex_p_iv.setOnClickListener(this);
        bir1_p_iv.setOnClickListener(this);
        height_p_iv.setOnClickListener(this);
        weight_p_iv.setOnClickListener(this);
        zhiye_p_tv.setOnClickListener(this);
        zhiye_p_iv.setOnClickListener(this);
    }

    private void FV() {
        top_left_iv=findViewById(R.id.top_left_iv);
        title_tv=findViewById(R.id.m_title_tv);
        rightTitle_tv=findViewById(R.id.m_righttitle_tv);

        p_top_pic=findViewById(R.id.p_top_pic);

        ViewUtils.setTextViewStr(title_tv,"个人信息");
        ViewUtils.setTextViewStr(rightTitle_tv,"完成");

        sex2_p_tv=findViewById(R.id.sex2_p_tv);
        zhiye_p_tv=findViewById(R.id.zhiye_p_tv);
        bir2_p_tv=findViewById(R.id.bir2_p_tv);
        height2_p_tv=findViewById(R.id.height2_p_tv);
        weight2_p_tv=findViewById(R.id.weight2_p_tv);

        sex_p_iv=findViewById(R.id.sex_p_iv);
        zhiye_p_iv=findViewById(R.id.zhiye_p_iv);
        bir1_p_iv=findViewById(R.id.bir1_p_iv);
        height_p_iv=findViewById(R.id.height_p_iv);
        weight_p_iv=findViewById(R.id.weight_p_iv);

    }

    @Override
    public void onClick(View v) {
        int vid=v.getId();
        if (vid==R.id.top_left_iv){
            back();
        }else if (vid==R.id.m_righttitle_tv){
            complete();
        }else if (vid==R.id.sex2_p_tv||vid==R.id.sex_p_iv){
            showSexDialog();
        }else if (vid==R.id.zhiye_p_tv||vid==R.id.zhiye_p_iv){
            showProfessionDialog();
        }else if (vid==R.id.bir2_p_tv||vid==R.id.bir1_p_iv){
            showBirDialog();
        }else if (vid==R.id.height2_p_tv||vid==R.id.height_p_iv){
            showHeightDialog();
        }else if (vid==R.id.weight2_p_tv||vid==R.id.weight_p_iv){
            showWeightDialog();
        }
    }

    private void showWeightDialog() {

        weight_p_iv.setImageResource(R.drawable.ic_shangla);
        WeightSelectDialog weightSelectDialog=new WeightSelectDialog(this);
        weightSelectDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                weight_p_iv.setImageResource(R.drawable.ic_xiala);
            }
        });
        float weightValue=UserSpUtil.readFloat(NbConstants.User.weight);
        weight=weightValue;
        weightSelectDialog.show(weightValue);
        weightSelectDialog.setValueChange(new WeightSelectDialog.ValueChange() {
            @Override
            public void change(float value) {
                LogUtils.i(TAG,"--weightSelectDialog--setValueChange--value = "+value);
                weight = value;
            }
        });

        weightSelectDialog.setBackgroundColor(getResources().getColor(R.color.themeColor));
        weightSelectDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {}//内部已经处理了

            @Override
            public void ok(Dialog dialogView) {
                if (weight2_p_tv!=null){weight2_p_tv.setText(weight+" Kg");}
            }
        });

    }


    private void showProfessionDialog() {


        zhiye_p_iv.setImageResource(R.drawable.ic_shangla);
        WheelViewDialog wheelViewDialog = new WheelViewDialog(this);
        wheelViewDialog.setGravity(Gravity_BUTTOM);
        List<String> list=new ArrayList<>();
        list.add("普通工作者");
        list.add("运动员");
        wheelViewDialog.setOffeset(1);
        wheelViewDialog.setItems(list);
        wheelViewDialog.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                Log.i(TAG, "selectedIndex: " + selectedIndex + ", item: " + item);
                if (selectedIndex==1){
                    profession=0;
                }else if (selectedIndex==2){
                    profession=1;//运动员
                }
            }
        });
        wheelViewDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }
            @Override
            public void ok(Dialog dialogView) {
                dialogView.cancel();
                if (zhiye_p_tv!=null)zhiye_p_tv.setText(profession==0?"普通工作者":"运动员");

            }
        });
        wheelViewDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (zhiye_p_iv!=null) zhiye_p_iv.setImageResource(R.drawable.ic_xiala);
            }
        });
        wheelViewDialog.setSelectPosition((profession==0?0:1));
        wheelViewDialog.show();
        wheelViewDialog.setBackgroundColor(getResources().getColor(R.color.themeColor));

    }

    private void showBirDialog() {

        bir1_p_iv.setImageResource(R.drawable.ic_shangla);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String time = sdf.format(new Date());
        String date = time.split(" ")[0];
        CustomDatePicker datePicker= new CustomDatePicker(this,new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) {
                LogUtils.i(TAG,time);
                birthday = time.substring(0,10);


            }
        }, "1945-01-01 00:00", time);
        // datePicker.setSelectedCalender("1991-06-13");
        datePicker.showSpecificTime(false); //显示时和分
        datePicker.setIsLoop(false);
        datePicker.setDayIsLoop(true);
        datePicker.setMonIsLoop(true);
        datePicker.show(date);

        datePicker.setSelectedTime(birthday);

        datePicker.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (bir1_p_iv!=null){
                    bir1_p_iv.setImageResource(R.drawable.ic_xiala);
                }
            }
        });
        datePicker.setBackgroundColor(getResources().getColor(R.color.themeColor));
        datePicker.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {}//内部已经处理了

            @Override
            public void ok(Dialog dialogView) {
                // birth_cb6.setTitle2(birthday);
                if (bir2_p_tv!=null){bir2_p_tv.setText(birthday);}

            }
        });






    }


    private void showHeightDialog() {
        height_p_iv.setImageResource(R.drawable.ic_shangla);
        WheelViewDialog wheelViewDialog = new WheelViewDialog(this);
        wheelViewDialog.setGravity(Gravity_BUTTOM);
        List<String> list=new ArrayList<>();
        for (int i=100;i<230;i++){
            list.add(i+" CM");
        }
        wheelViewDialog.setOffeset(1);
        wheelViewDialog.setItems(list);
        wheelViewDialog.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                Log.i(TAG, "selectedIndex: " + selectedIndex + ", item: " + item);
                height = NumUtils.string2Int(item.replace(" CM",""));
            }
        });
        wheelViewDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }
            @Override
            public void ok(Dialog dialogView) {
                dialogView.cancel();
                if (height2_p_tv!=null)height2_p_tv.setText(height+" CM");
            }
        });
        wheelViewDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (height_p_iv!=null) height_p_iv.setImageResource(R.drawable.ic_xiala);
            }
        });
        wheelViewDialog.setSelectPosition(height-100);
        wheelViewDialog.show();
        wheelViewDialog.setBackgroundColor(getResources().getColor(R.color.themeColor));

    }

    private void showSexDialog() {
        sex_p_iv.setImageResource(R.drawable.ic_shangla);
        WheelViewDialog wheelViewDialog = new WheelViewDialog(this);
        wheelViewDialog.setGravity(Gravity_BUTTOM);
        List<String> list=new ArrayList<>();
        list.add("男");
        list.add("女");
        wheelViewDialog.setOffeset(1);
        wheelViewDialog.setItems(list);
        wheelViewDialog.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                Log.i(TAG, "selectedIndex: " + selectedIndex + ", item: " + item);
                sex = selectedIndex-1;
            }
        });
        wheelViewDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }
            @Override
            public void ok(Dialog dialogView) {
                dialogView.cancel();
                if (sex2_p_tv!=null)sex2_p_tv.setText(sex==0?"男":"女");
                if (p_top_pic!=null)p_top_pic.setImageResource(sex==0?R.drawable.ic_man:R.drawable.ic_women);
            }
        });
        wheelViewDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (sex_p_iv!=null) sex_p_iv.setImageResource(R.drawable.ic_xiala);
            }
        });
        wheelViewDialog.setSelectPosition(sex);
        wheelViewDialog.show();
        wheelViewDialog.setBackgroundColor(getResources().getColor(R.color.themeColor));

    }

    private void complete() {

        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        //role=0普通 role=1 运动员
        final int profession2 = profession;
        String dtype = "0";
        final float weight2 = weight;
        final int sex2=sex;
        final String bir=birthday;
        final int height2=height;

        if (weight<=0){
            ToastUtil.show("体重必须大于0");
            return;
        }
        if (height2<=0){
            ToastUtil.show("身高必须大于0");
            return;
        }

        //0-非体力劳动者，1-体力劳动者，2-非运动员，3-运动员
        NbNetUtils.getInstance().updateUserInfo(this, NbUtitls.getLoginAuthkey(), null, sex2, bir,
                height2, weight2, profession2, dtype, new HttpResponse2<NbBaseResponse>() {
                    @Override
                    public void result(boolean isRequestSuccess, String requestResultDesc, int requestResultCode,
                                       NbBaseResponse nbBaseResponse, String s) {
                        LogUtils.i(TAG,s);
                        if (isRequestSuccess&&requestResultCode==1){
                            //成功
                            ToastUtil.show("设置用户信息成功");
                            saveUserInfo(sex2,bir,height2,profession2,weight2);
                            setResult(2);
                            finish();
                        }else {
                            //失败
                            ToastUtil.show("设置用户信息失败");
                        }

                    }
                });



    }

    private void saveUserInfo(int sex2, String bir, int height2,int profession,float weight) {
        UserSpUtil.writeString(NbConstants.User.birthday,bir);//yyyy-MM-dd
        UserSpUtil.writeInt(NbConstants.User.height,height2);
        UserSpUtil.writeInt(NbConstants.User.sex,sex2);
        UserSpUtil.writeInt(NbConstants.User.profession,profession);//0普通1运动员
        UserSpUtil.writeFloat(NbConstants.User.weight,weight);
    }

    private void back() {
        finish();
    }
}
